$(function () {
    var showPersonListUrl = '/backstage/person/listByCondition';
    //页码
    var pageIndex = 1;
    //定义一页显示10行数据
    var pageSize = 10;
    //总用户数
    var maxItem = 999;
    //总页数
    var totalPage = 999;
    var name = '';

    showPersonList('',pageIndex,pageSize);

    function showPersonList(name,pageIndex,pageSize) {
        $('#name').val(name);
        var url = showPersonListUrl + '?pageIndex='+pageIndex+'&pageSize='+pageSize+'&name='+name;
        $.ajax({
            url:url,
            type:'POST',
            contentType:false,
            processData:false,
            cache:false,
            beforeSend: function(request){
                request.setRequestHeader("Token",localStorage.getItem('token'));
            },
            success: function (data) {
                console.log(data);
                if (data.success) {
                    totalPage = data.data.pages;
                    maxItem = data.data.total;
                    var personList = '';
                    data.data.records.map(function (item,index) {
                        var role = '';
                        var status = '';
                        if (item.role=='0'){
                            role = '普通用户'
                        }else if (item.role == '1') {
                            role = '超级管理员'
                        }else if (item.role == '2') {
                            role = '用户管理员'
                        }else if (item.role == '3') {
                            role = '动态管理员'
                        }
                        if (item.status == '0') {
                            status = '启用'
                        }else{
                            status = '停用'
                        }
                        var classStatus = '';
                        if (item.status=='1'){
                            classStatus = 'layui-btn-disabled';
                        }
                        personList += '<tr><td><input type="checkbox" name="id" value="'+item.userId+'" lay-skin="primary">'+
                            '</td><td>'+item.userId+'</td>'+'<td>'+item.username+'</td><td>'+item.gender+'</td><td>'+item.phone+
                            '</td><td>'+role+'</td><td class="td-status"><span class="layui-btn layui-btn-normal layui-btn-mini '+classStatus+'">'+status+'</span></td>'+
                            '<td class="td-manage"><a onclick="member_stop(this,\''+item.userId+'\')" href="javascript:;"  title="'+status+'">'+
                            '<i class="layui-icon">&#xe601;</i></a><a title="编辑"  onclick="xadmin.open(\'详情\',\'/admin/person/person-show?userId='+item.userId+'\',600,600)" href="javascript:;">'+
                            '<i class="layui-icon">&#xe642;</i></a>'+ '</a><a title="删除" onclick="member_del(this,\''+item.userId+'\')" href="javascript:;">'+
                            '<i class="layui-icon">&#xe640;</i></a></td></tr>';
                    })
                    $('#personList').html(personList);
                    $('#current').text(pageIndex);
                    $('.end').text(totalPage);
                }else{
                    if (data.code == 403) {
                        //权限不够
                        $('.x-nav').html('<h1 style="color: red">'+data.message+'</h1>');
                        $('.layui-fluid').hide();
                    }
                }

            }
        })
    }


    //跳转到指定页码
    $('#forward').click(function () {
        var page = $('#page').val();
        if (page) {
            if (page < 0 || page > totalPage) {
                alert("填写的页码不规范");
            }else {
                pageIndex = page;
                showPersonList(name,pageIndex,pageSize);
                $('#page').val('');
            }
        }else {
            alert("填写的页码为空");
        }

    });

    //搜索员工内容
    $('#searchOrder').click(function () {
        name = $('#name').val();
        showPersonList(name,pageIndex,pageSize);
    });

    //到最后一页
    $('.page .end').click(function () {
        var page = $(this).text();
        pageIndex = page;
        showPersonList(name,pageIndex,pageSize);
    });
    //前一页
    $('.layui-card-body .page .prev').click(function () {
        //如果当前页数为1则不能往前一页
        if (pageIndex>1){
            pageIndex = pageIndex-1;
            showPersonList(name,pageIndex,pageSize);
        }
    });
    //往后一页
    $('.page .next').click(function () {
        //如果当前页数为最后一页则不能往后一页
        if (pageIndex < totalPage) {
            pageIndex = pageIndex + 1;
            showPersonList(name,pageIndex,pageSize)
        }
    });


});
